package py.com.fms.ven.persistence.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import py.com.fms.ven.bean.SalesHeadBean;
import py.com.fms.ven.bean.SalesDetBean;
import py.com.fms.exception.CommonException;
import py.com.fms.fms.bean.PersonBean;
import py.com.fms.persistence.services.FmsServices;

@SuppressWarnings("unused")
public class SalesServices extends FmsServices {

	public SalesServices(SqlMapClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 [[[
	 [[ The following method retrieve a list of all records
	 [[ Normally used to load a grid.
	 */
	
	/**
	 * Returns an List object with all data of the Entity Header. 
	 *
	 * @return      a list with all data
	 */
	@SuppressWarnings("unchecked")
	public List<SalesHeadBean> getAllSaleHead() throws CommonException{
		List<SalesHeadBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<SalesHeadBean>)this.getConfig().queryForList("sales.getAllSalesHead");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns an List object with all data of the Entity Detail. 
	 *
	 * @return      a list with all data
	 */
	@SuppressWarnings("unchecked")
	public List<SalesDetBean> getAllSaleDet() throws CommonException{
		List<SalesDetBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<SalesDetBean>)this.getConfig().queryForList("sales.getAllSalesDet");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an List of Details for a Header Id. 
	 * The argument must be a Long Object with the value of the header Id
	 *
	 * @param  		value of the header id
	 * @return      a list with the details for the passed argument (header Id)
	 */
	@SuppressWarnings("unchecked")
	public List<SalesDetBean> getSalesDetByHeadId(Long headId) throws CommonException{
		List<SalesDetBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<SalesDetBean>)this.getConfig().queryForList("sales.getSalesDetByHeadId", headId);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns an specific header object. 
	 * The argument must be a Long Object with the value of the header Id
	 *
	 * @param  		value of the header id
	 * @return      the header object for the passed argument (id)
	 */
	public SalesHeadBean getSalesHeadById(Long id) throws CommonException{
		SalesHeadBean queryForObject;
		try {
			
			//[[ queryForObject is an implemented interface of iBatis ]] 
			
			queryForObject = (SalesHeadBean)this.getConfig().queryForObject("sales.getSalesHeadById", id);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an specific header object. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the enterprise id
	 * - A long Object containing the sales number
	 * 
	 * @param  		the enterprise id
	 * @param  		transaction type
	 * @param  		establishment
	 * @param  		expedition point
	 * @param  		voucher
	 * @return      the header object for the passed arguments
	 */
	public Long getSalesHeadIdByEntSalesUk(Long idEnt, Long idTypeTrans, String establishment, String expedition, String voucher, Long idCustomer) throws CommonException, SQLException{
		Long queryForObjectLong;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idEnt", idEnt);
			param.put("idTypeTrans", idTypeTrans);
			param.put("establishment", establishment);
			param.put("expedition", expedition);
			param.put("voucher", voucher);
			param.put("idCustomer", idCustomer);
			queryForObjectLong = (Long)this.getConfig().queryForObject("sales.getSalesHeadIdByEntSalesUk", param);
			return queryForObjectLong;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns Pending Sales. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A String object containing the purchase status
	 * - A Date object containing the purchase date
	 * 
	 * @param  		the supplier id
	 * @param  		the purchase status
	 * @param  		the purchase Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<SalesHeadBean> getFilteredSales(Long idCustomer, String status, Date voucherDate) throws CommonException, SQLException{
		List<SalesHeadBean> queryForList;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idCustomer", idCustomer);
			param.put("status", status);
			param.put("voucherDate", voucherDate);
			queryForList = (List<SalesHeadBean>)this.getConfig().queryForList("sales.getFilteredSales", param);
			return queryForList;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns Pending sales. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A Date object containing the purchase date
	 * 
	 * @param  		the supplier id
	 * @param  		the purchase Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<SalesHeadBean> getPendingSales(Long idCustomer, Date voucherDate, String status) throws CommonException, SQLException{
		List<SalesHeadBean> queryForList;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idCustomer", idCustomer);
			param.put("voucherDate", voucherDate);
			param.put("status", status);
			queryForList = (List<SalesHeadBean>)this.getConfig().queryForList("sales.getPendingSales", param);
			return queryForList;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	/**
	 * Insert a Header Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the header Fields
	 * 
	 * @param  		the header entity bean(SalesHeadBean)
	 */
	public void saveNewSaleHead(SalesHeadBean salesHead) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("sales.insSalesHead", salesHead);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 * Insert Detail Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the detail fields including the header id
	 * 
	 * @param  		the detail entity bean(SalesDetBean)
	 */
	public void saveNewSaleDet(SalesDetBean salesDet) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("sales.insSalesDet", salesDet);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	/**
	 * Update a Header Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the header Fields
	 * 
	 * @param  		the header entity bean(SalesHeadBean)
	 */
	public void updateSaleHead(SalesHeadBean salesHead) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("sales.udpSalesHeadById", salesHead);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	/**
	 * update a Detail Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the detail fields including the header id
	 * 
	 * @param  		the detail entity bean(SalesDetBean)
	 */
	public void updateSaleDet(SalesDetBean salesDet) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("sales.udpSalesDetById", salesDet);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	/**
	 * Confirm a specific Sale. 
	 * The passed argument is:
	 * <p>
	 * - A Long object with the Sale id
	 * 
	 * @param  		the Sale id
	 */
	public void confirmSale(Long idSale) throws CommonException{
		try {
			this.getConfig().startTransaction();
			/*cambiar de estado a la compra*/
			this.getConfig().update("sales.confirmSalesById", idSale);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 * Cancel a specific Sale. 
	 * The passed argument is:
	 * <p>
	 * - A Long object with the Sale id
	 * 
	 * @param  		the Sale id
	 */
	public void cancelSale(Long idSale) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("sales.cancelSalesById", idSale);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}

}
